/*
* Copyright 2010 Dovado FZ-LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#import <UIKit/UIKit.h>
#import "DialogUtils.h"
 

@implementation DialogUtils


+ (UIAlertView*) createProgressionAlertWithTitle:(NSString *)title andMessage: (NSString*) message
{
	UIAlertView* mProgressAlert = [[UIAlertView alloc] initWithTitle: title
												message: message
											   delegate: self
									  cancelButtonTitle: nil
									  otherButtonTitles: nil];
	UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
	activityView.frame = CGRectMake(139.0f-18.0f, 80.0f, 37.0f, 37.0f);
	[mProgressAlert addSubview:activityView];
	[activityView startAnimating];
	[activityView release];
	
	[mProgressAlert show];	
	return mProgressAlert;
}

+ (void) dismissAlert: (UIAlertView*) alert {
	if (alert != NULL) {
		[alert dismissWithClickedButtonIndex:0 animated:TRUE];
		[alert release];
	} else {
		NSLog(@"Warning - tried to dismiss dialog with null-handle");
	}

}

+ (void) showConnectionFailedDialog: (request_error_codes_t) errorCode {
	NSString* dialogTitle = NSLocalizedString(@"network_exception_dialog_title", @"Network Error");
	NSString* dialogMessage = NULL;
	switch (errorCode) {
		case ILLEGAL_HOST:
			dialogMessage = NSLocalizedString(@"illegal_host_message", @"Illegal host");
			break;
		case ILLEGAL_USER:
			dialogMessage = NSLocalizedString(@"illegal_user_message", @"Illegal user");
			break;
		case ILLEGAL_PASSWORD:
			dialogMessage = NSLocalizedString(@"illegal_password_message", @"Illegal password");
			break;
		case ILLEGAL_ALIAS:
			dialogMessage = NSLocalizedString(@"illegal_alias_message", @"Illegal alias");
			break;
		case ILLEGAL_DIM_VALUE:
			dialogMessage = NSLocalizedString(@"illegal_dimmer_value_message", @"Dimmer value out of range");
			break;
		default:
			NSLog(@"Received unkown errorcode %d when doing networking", errorCode);
			break;
	}
	
    UIAlertView *errorAlert = [[UIAlertView alloc]
							   initWithTitle: dialogTitle
							   message: dialogMessage
							   delegate:nil
							   cancelButtonTitle:NSLocalizedString(@"ok_button_text", @"Ok")
							   otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    NSLog (@"Login failed alert");
}

+ (void) showOnConnectionResultErrorDialog: (request_listener_error_t) errorCode {
	NSString* dialogTitle = NSLocalizedString(@"on_result_exception_dialog_title", @"Network Error");
	NSString* dialogMessage = NULL;
	switch (errorCode) {
		case ERROR_UNKNOWN_USER:
			dialogMessage = NSLocalizedString(@"error_unkown_user", @"Unknown user and/or password");
			break;
		case ERROR_ANOTHER_ACTIVE_API:
			dialogMessage = NSLocalizedString(@"error_another_active_api", @"Another user is currently logged in");
			break;
		case ERROR_UNKNOWN_ERROR:
			dialogMessage = NSLocalizedString(@"error_unknown_error", @"Unknown error");
			break;
		case ERROR_UNABLE_TO_CLOSE_CONNECTION:
			dialogMessage = NSLocalizedString(@"error_unable_to_close_connection", @"Unable to close connection");
			break;		
		default:
			NSLog(@"Received unkown errorcode %d when doing networking", errorCode);
			break;
	}
	
    UIAlertView *errorAlert = [[UIAlertView alloc]
							   initWithTitle: dialogTitle
							   message: dialogMessage
							   delegate:nil
							   cancelButtonTitle:NSLocalizedString(@"ok_button_text", @"Ok")
							   otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    NSLog (@"Login failed alert");
}

+ (void) showLoginFailedDialog {
    UIAlertView *errorAlert = [[UIAlertView alloc]
							   initWithTitle: NSLocalizedString(@"login_failed_alert_title", @"Login failed")
							   message:  NSLocalizedString(@"login_failed_alert_message", @"Supplied host/password was not accepted")
							   delegate:nil
							   cancelButtonTitle:NSLocalizedString(@"ok_button_text", @"Ok")
							   otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    NSLog (@"Login failed alert");
}

+ (void) showNotSupportedDialog {
	UIAlertView *errorAlert = [[UIAlertView alloc]
							   initWithTitle: NSLocalizedString(@"not_supported_alert_title", @"Not supported")
							   message:  NSLocalizedString(@"not_supported_alert_message", @"This feature is not supported for this router")
							   delegate:nil
							   cancelButtonTitle:NSLocalizedString(@"ok_button_text", @"Ok")
							   otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    NSLog (@"Login failed alert");
}

+ (void) showDialog:(NSString *)title andMessage: (NSString*) message {
	UIAlertView *errorAlert = [[UIAlertView alloc]
							   initWithTitle: title
							   message: message
							   delegate:nil
							   cancelButtonTitle:NSLocalizedString(@"ok_button_text", @"Ok")
							   otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    NSLog (@"Login failed alert");
}
@end
